package com.china08.yxyapi.api.home.kecheng.sync;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSynchroCatalog;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSynchroGrade;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSynchroLesson;
import com.china08.yxyapi.entity.db.res.YxyResSubject;
import com.china08.yxyapi.entity.db.res.YxyResVersion;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsSynchroCatalogRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSynchroGradeRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSynchroLessonRepository;
import com.china08.yxyapi.repository.db.YxyResSubjectRepository;
import com.china08.yxyapi.repository.db.YxyResVersionRepository;

/**
 * 同步课程--通过课程名称模糊
 * 
 * @author LiuWeidong
 *
 */
@CrossOrigin
@RestController
public class KechengSyncContainingSrhController extends YxyApiBaseController {

    @Autowired
    private YxyLessonsSynchroLessonRepository yxyLessonsSynchroLessonRepository;

    @Autowired
    private YxyLessonsSynchroCatalogRepository yxyLessonsSynchroCatalogRepository;

    /** 学科 */
    @Autowired
    private YxyResSubjectRepository yxyResSubjectRepository;

    /** 版本 */
    @Autowired
    private YxyResVersionRepository yxyResVersionRepository;

    /** 分类 */
    @Autowired
    private YxyLessonsSynchroGradeRepository yxyLessonsSynchroGradeRepository;

    @RequestMapping(value = "/api_yw/home/kecheng/sync/containing/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> find(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel reqModel) {

        String name = reqModel.name;
        Pageable pageable = reqModel.getPageable(Direction.DESC, "createdDate");
        List<RespModel> retuLst = new ArrayList<>();

        Page<YxyLessonsSynchroLesson> page = yxyLessonsSynchroLessonRepository
                .findByStatusAndIsVisibleAndNameContaining(2, 1, name, pageable);

        for (YxyLessonsSynchroLesson yxyLessonsSynchroLesson : page) {
            RespModel model = new RespModel();

            String catalogId = yxyLessonsSynchroLesson.getCatalogId();
            YxyLessonsSynchroCatalog yxyLessonsSynchroCatalogs = yxyLessonsSynchroCatalogRepository.findById(catalogId);
            if (null != yxyLessonsSynchroCatalogs) {
                String gradeId1 = yxyLessonsSynchroCatalogs.getGradeId();
                String versionId1 = yxyLessonsSynchroCatalogs.getVersionId();
                String subjectId1 = yxyLessonsSynchroCatalogs.getSubjectId();
                YxyLessonsSynchroGrade yxyLessonsSynchroGrades = yxyLessonsSynchroGradeRepository.findById(gradeId1);
                YxyResSubject yxyResSubjects = yxyResSubjectRepository.findById(subjectId1);
                YxyResVersion yxyResVersions = yxyResVersionRepository.findById(versionId1);
                model.gradeName = yxyLessonsSynchroGrades.getName();
                model.subjectName = yxyResSubjects.getName();
                model.versionName = yxyResVersions.getName();
            }

            model.vip = yxyLessonsSynchroLesson.getVip();
            model.cover = yxyLessonsSynchroLesson.getCover();
            model.count = yxyLessonsSynchroLesson.getCount();
            model.duration = yxyLessonsSynchroLesson.getDuration();
            model.name = yxyLessonsSynchroLesson.getName();
            model.ep = yxyLessonsSynchroLesson.getEp();
            model.vv = yxyLessonsSynchroLesson.getVv();
            model.lessonId = yxyLessonsSynchroLesson.getId();

            retuLst.add(model);

        }
        Page<RespModel> page1 = new PageImpl<RespModel>(retuLst, pageable, page.getTotalElements());
        return page1;
    }

    /* 请求参数 */
    protected static class ReqModel extends YxyApiPageReqModel {

        // 课程名称
        public String name;
    }

    /* 响应参数 */
    protected static class RespModel extends YxyApiRespModel {

        // 学年
        public String gradeName;
        // 学科
        public String subjectName;
        // 版本
        public String versionName;

        // 是否vip 0--非vip 1--vip课程
        public Integer vip;
        // 封面
        public String cover;
        // 总课时
        public Integer count;
        // 总时长
        public Integer duration;
        // 课程名称
        public String name;
        // 课程评分（所有评分之和处于评分人数即综合评分）
        public Float ep;
        // 学习人数
        public Integer vv;
        // 课程Id
        public String lessonId;

    }

}
